2023/12/231788字符

内置模块

os

const os = require('os');  // 引入内置模块

os.EOL;  // 回车

os.arch();  //--> x64  操作系统架构名

os.cpus();  // cpu 信息

os.freemem();  // 内存剩余

os.homedir();  //--> 'C:\\Users\\bozai'  用户目录

os.hostname();  //--> 'DESKTOP-U10FO12'  主机名

os.tmpdir();  //--> 'C:\\Users\\bozai\\AppData\\Local\\Temp  临时目录

path

const path = require('path');

path.basename('blog/bozai/index.html');  //--> index.html
path.basename('blog/bozai/index.html', '.html');  //--> index

path.sep  //--> \  操作系统分割符

path.delimiter  // 环境变量分割符

path.extname('init.js');  //--> .js  文件后缀名

path.join('a', 'b', '../', 'init.js');  //--> a\init.js  路径拼接,分割符会按当前操作系统的分割符进行分割

path.relative('a/b/c/1.js', 'a/b/d/2.js');  //--> ..\..\d\2.js

path.resolve('111.js');  //--> D:\node\111.js  从根目录下寻找

URL

const URL = require('url');

const url = new URL.URL('http://root:password@127.0.0.1:5500/note?a=1&b=2#id');  // 类似于 location
console.log(url);  //--> 
/* URL {
  href: 'http://bozai:pass@127.0.0.1:5500/note?a=1&b=2#id',
  origin: 'http://127.0.0.1:5500',
  protocol: 'http:',
  username: 'root',
  password: 'password',
  host: '127.0.0.1:5500',
  hostname: '127.0.0.1',
  port: '5500',
  pathname: '/note',
  search: '?a=1&b=2',
  searchParams: URLSearchParams { 'a' => '1', 'b' => '2' },
  hash: '#id'
} */
console.log(url.searchParams.has('a'));  //--> true
console.log(url.searchParams.get('a'));  //--> 1

const obj = {
  protocol: 'http:',
  host: '127.0.0.1',
}
console.log(URL.format(obj));  //--> http://127.0.0.1